package com.elinkway.infinitemovies.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.MoviesConstant;
import com.le123.ysdq.R;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by zhaopeipei on 2016/5/26.
 */
public class DownloadSharpnessSelectActivity extends BaseSecondaryActivity implements View.OnClickListener {

    private static final String TAG = "DownloadSharpnessSelectActivity";
    private static final int RQUEST_CODE_DOWNLOAD_SHARPNESS = 2;
    private RelativeLayout mRelativeCommonSharpness;
    private RelativeLayout mRelativeHighSharpness;
    private RelativeLayout mRelativeSuperSharpness;
    private RelativeLayout mRelativeSuper2Sharpness;
    private ImageView mCommonSharpness;
    private ImageView mHighSharpness;
    private ImageView mSuperSharpness;
    private ImageView mSuper2Sharpness;
    private String mCurrentSharpnessSelect;
    private String mCurrentGender;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sharpness_download_modify);
        initActionBar();
        mLeftBackIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra(MoviesConstant.DOWNLOAD_SHARPNESS_SELECT, mCurrentGender);
                setResult(RQUEST_CODE_DOWNLOAD_SHARPNESS, intent);
                finish();
            }
        });
        mTitleTv.setText(getResources().getString(R.string.download_sharpness_select));
        initView();
        setListener();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.user_message, menu);
//        MenuItem item = menu.findItem(R.id.action_sure);
//        View mView = MenuItemCompat.getActionView(item);
//        mView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//            }
//        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // TODO Auto-generated method stub
        if(item.getItemId() == android.R.id.home)
        {
            Intent intent = new Intent();
            intent.putExtra(MoviesConstant.DOWNLOAD_SHARPNESS_SELECT, mCurrentGender);
            setResult(RQUEST_CODE_DOWNLOAD_SHARPNESS, intent);
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        mRelativeCommonSharpness = (RelativeLayout) findViewById(R.id.download_sharpness_common_select);
        mRelativeHighSharpness = (RelativeLayout) findViewById(R.id.download_sharpness_high_select);
        mRelativeSuperSharpness = (RelativeLayout) findViewById(R.id.download_sharpness_super_select);
        mRelativeSuper2Sharpness = (RelativeLayout) findViewById(R.id.download_sharpness_super2_select);
        mCommonSharpness = (ImageView) findViewById(R.id.iv_sharpness_common_select);
        mHighSharpness = (ImageView) findViewById(R.id.iv_sharpness_high_select);
        mSuperSharpness = (ImageView) findViewById(R.id.iv_sharpness_super_select);
        mSuper2Sharpness = (ImageView) findViewById(R.id.iv_sharpness_super2_select);
        LogUtils.e(TAG, "DOWNLOAD_SHARPNESS_SELECT " + getIntent().getIntExtra(MoviesConstant.DOWNLOAD_PATH_SELECT, 1));
        genderShow(Integer.parseInt(getIntent().getStringExtra(MoviesConstant.DOWNLOAD_SHARPNESS_SELECT)));

    }

    private void setListener() {
        mRelativeCommonSharpness.setOnClickListener(this);
        mRelativeHighSharpness.setOnClickListener(this);
        mRelativeSuperSharpness.setOnClickListener(this);
        mRelativeSuper2Sharpness.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        int gender = Integer.valueOf("".equals(getIntent().getStringExtra(MoviesConstant.DOWNLOAD_SHARPNESS_SELECT))
                ? MoviesConstant.SUPER : getIntent().getStringExtra(MoviesConstant.DOWNLOAD_SHARPNESS_SELECT));
        genderShow(gender);
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.download_sharpness_common_select:
                genderShow(0);
                finishAtNow();
                break;
            case R.id.download_sharpness_high_select:
                genderShow(1);
                finishAtNow();
                break;
            case R.id.download_sharpness_super_select:
                genderShow(2);
                finishAtNow();
                break;
            case R.id.download_sharpness_super2_select:
                genderShow(3);
                finishAtNow();
                break;
        }
    }

    public void finishAtNow() {
        Intent intent = new Intent();
        intent.putExtra(MoviesConstant.DOWNLOAD_SHARPNESS_SELECT, mCurrentGender);
        setResult(RQUEST_CODE_DOWNLOAD_SHARPNESS, intent);
        finish();
    }

    public void genderShow(int gender) {
        if (0 == gender) {
            mCommonSharpness.setVisibility(View.VISIBLE);
            mHighSharpness.setVisibility(View.GONE);
            mSuperSharpness.setVisibility(View.GONE);
            mSuper2Sharpness.setVisibility(View.GONE);
            mCurrentGender = MoviesConstant.NORMAL;
        } else if(1 == gender) {
            mCommonSharpness.setVisibility(View.GONE);
            mHighSharpness.setVisibility(View.VISIBLE);
            mSuperSharpness.setVisibility(View.GONE);
            mSuper2Sharpness.setVisibility(View.GONE);
            mCurrentGender = MoviesConstant.HIGH;
        } else if (2 == gender) {
            mCommonSharpness.setVisibility(View.GONE);
            mHighSharpness.setVisibility(View.GONE);
            mSuperSharpness.setVisibility(View.VISIBLE);
            mSuper2Sharpness.setVisibility(View.GONE);
            mCurrentGender = MoviesConstant.SUPER;
        } else if (3 == gender) {
            mCommonSharpness.setVisibility(View.GONE);
            mHighSharpness.setVisibility(View.GONE);
            mSuperSharpness.setVisibility(View.GONE);
            mSuper2Sharpness.setVisibility(View.VISIBLE);
            mCurrentGender = MoviesConstant.SUPER2;
        }
    }

}
